<template>
  <div>
    <TopFilter ref="topFilter" :list="topFilterList" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle" />
    <FilterTable
      ref="table"
      :columns="columns"
      columnsRef="partPartOtherRepairPartBuyInvoicIndex"
      :fetchapi="fetchApi"
      :params="fetchParams"
      selectionType="single"
      :datakey="'rows'"
      :isSelectColumn="true"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
    >
      <template slot="moreActions">
        <span @click="() => this.deleteInvoices()">删除数据</span>
      </template>
      <template slot="controls">
        <el-button icon="el-icon-plus" type="primary" @click="openAddForm('add')">新 建</el-button>
        <!-- <el-button>删 除</el-button> -->
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="showDetail" title="明细" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <Detail ref="Detail" :authId="selectAuthId" @close="closeDetail" />
    </BaseDialog>
    <Drawer :visible.sync="showAddForm" :title="dialogTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm ref="AddForm" :Type="AddFormType" :AuthId="selectAuthId" @close="closeAddForm" @changeType="changeType" />
    </Drawer>
  </div>
</template>

<script>
/**
 * @Author: 刘加明
 **/
import moment from 'moment';
import { dictionary } from '@/utils/dictMixin';
import { getPartInvoice, getProvider, deleteInvoices } from '@/api/part/partOther/repairPartBuyInvoic';
import Detail from '@/pages/part/partOther/repairPartBuyInvoic/detail.vue';
import AddForm from '@/pages/part/partOther/repairPartBuyInvoic/addForm.vue';
import { confirmAction } from '@/utils/index.js';

export default {
  name: 'repairPartBuyInvoic',
  components: {
    Detail,
    AddForm
  },
  mixins: [dictionary],
  data() {
    this.conf = { add: '新增', edit: '编辑', show: '显示' };
    return {
      topFilterList: this.createtopFilterList(),
      columns: this.createTableColumns(),
      fetchParams: { xhrAbort: true },
      fetchApi: getPartInvoice,
      onChangeColumn: [],
      showAddForm: false,
      showDetail: false,
      dialogTitle: null,
      AddFormType: null,
      selectAuthId: null,
      receiptStatus: { '1001': '录入', '1002': '提交' } // 单据状态
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.getProvider(); // 供应商下拉
  },
  methods: {
    createtopFilterList() {
      return [
        {
          type: 'SELECT',
          label: '供应商',
          fieldName: 'providerCode',
          // itemList: this.createDictList('1004'),
          itemList: []
          // initialValue: 10041002
        },
        {
          type: 'SELECT',
          label: '单据状态',
          fieldName: 'type',
          itemList: [
            { text: '录入', value: '1001' },
            { text: '提交', value: '1002' }
          ]
        },
        {
          type: 'RANGE_DATE',
          label: '认证单日期',
          style: { minWidth: '200px' },
          fieldName: 'confirmDateStr|confirmDateEn'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 110,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.openAddForm('edit', props.row.AUTH_ID)}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.openDetail(props.row.AUTH_ID)}>
                  明细
                </el-button>
              </div>
            );
            // <el-button size="mini" type="text" onClick={() => this.deleteInvoices(props.row.AUTH_ID)}>
            //       删除
            //     </el-button>
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          }
        },
        {
          title: '单据编号',
          dataIndex: 'BILL_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单据状态',
          dataIndex: 'STATUS',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.receiptStatus[props.row.STATUS]}</span>;
          }
        },
        {
          title: '供应商代码',
          dataIndex: 'PROVIDER_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '供应商名称',
          dataIndex: 'CUSTOMER_NAME',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票类型',
          dataIndex: 'INVOICE_TYPE_NAME',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票号',
          dataIndex: 'INVOICE_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票日期',
          dataIndex: 'INVOICE_DATE',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '金额',
          dataIndex: 'AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '不含税金额',
          dataIndex: 'COST_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '税额',
          dataIndex: 'TAX',
          sorter: true,
          filter: true,
          filterType: 'number'
        }
      ];
    },
    // 表单展开收起后重新计算表格高度
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table.$refs.pageTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 获取供应商
    async getProvider() {
      const res = await getProvider();
      // console.log(res)
      this.topFilterList.find(item => item.fieldName === 'providerCode').itemList = res.data.rows.map(x => ({ value: x.customerCode, text: x.customerName }));
    },
    async deleteInvoices(authId) {
      await confirmAction('您将要删除单据编号为：' + this.onChangeColumn[0].BILL_NO + '，发票号为：' + this.onChangeColumn[0].INVOICE_NO + '的数据，是否继续？');
      if (!authId) {
        authId = this.onChangeColumn[0].AUTH_ID;
      }
      const res = await deleteInvoices(authId);
      // console.log(res)
      if (res.resultCode == 200) {
        this.$notify.success({ title: '提示信息', message: '删除成功!' });
        this.onChangeColumn = [];
        this.fetchParams = { ...this.fetchParams };
        this.columns = [...this.columns];
      }
    },
    // 重新请求表格数据
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
    },
    openDetail(data) {
      this.selectAuthId = data;
      this.showDetail = true;
    },
    closeDetail() {
      this.showDetail = false;
    },
    openAddForm(type, data) {
      this.AddFormType = type;
      this.selectAuthId = data || null;
      this.dialogTitle = `${this.conf[type]}维护配件采购发票认证`;
      this.showAddForm = true;
    },
    changeType(type) {
      console.log(type, 'changeTitle');
      this.dialogTitle = `${this.conf[type]}维护配件采购发票认证`;
    },
    closeAddForm() {
      this.fetchParams = { ...this.fetchParams };
      this.showAddForm = false;
    }
  }
};
</script>

<style lang="less" scoped></style>
